<?php defined('SYSPATH') or die('No direct script access.');

abstract class Controller_Common extends Controller_Template {

    public static $postfix;
    public $_config;
    public $sitetext;

	public function before() {
        parent::before();
        $this->_config = Kohana::$config->load('main');
        $this->sitetext = ORM::factory('Sitetext')->getAll($this::$postfix);
        View::set_global('title', $this->sitetext['comapny_name']['_value']);
        View::set_global('description', $this->sitetext['meta_description']['_value']);
        View::set_global('sitetext', $this->sitetext);

        if (!Cookie::get('lang', NULL)) {
            Cookie::set('lang', 'ru');
            View::set_global('lang', '');
            $this::$postfix = '';
        } else {
            View::set_global('lang', Cookie::get('lang', NULL));
            Cookie::get('lang', NULL) == 'ru' ? $this::$postfix = '' : $this::$postfix = '_'.Cookie::get('lang', NULL);
        }
	}

    public function buildCarcas() {
        $this->template->header = View::factory('pieces/header', array(
            'menu' => ORM::factory('Menu')->get_by_location(0),
            'c_url' => $c_uri = Request::detect_uri(),
        ));

        $this->template->footer = View::factory('pieces/footer');
    }

    public function getOffset($per_page) {
        $currentPage = $this->request->param('page');

        if ($currentPage == 1 || empty($currentPage)) {
            return 0;
        } else {
            return $currentPage * $per_page - $per_page;
        }
    }

    public function getType($str) {
        switch ($str) {
            case 'posts':
                return 0;
                break;
            case 'services':
                return 1;
                break;
            case 'projects':
                return 2;
                break;
            case 'partners':
                return 3;
                break;
        }
    }

}
